# -*- encoding=utf8 -*-
__author__ = "86181"

from airtest.core.api import *

auto_setup(__file__)

import os
import time 
from time import sleep

screen_status_on = 'adb  shell input keyevent 224'
screen_status_off = 'adb  shell input keyevent 223'
screen_status_cmd = 'adb shell "dumpsys deviceidle | grep mScreenOn"'
#screen_status_cmd = adb shell "dumpsys window | grep mScreenOn"
aiqiyi_activity = "com.qiyi.video.pad"
setting_activity = "com.android.settings"


def kill_activity():
    off_ststus = os.popen(screen_status_on)
    sleep(2)
    start_app(setting_activity)
    sleep(2)
    if exists(Template(r"tpl1699499537821.png", record_pos=(-0.044, 0.035), resolution=(1920, 1200))):
        touch((1732,999))

    for i in range(20):
        out = os.popen("adb shell dumpsys window | findstr mCurrentFocus").read()
        activity_str = out.split("u0")[1].split("/")[0]
        print("activity_str---", activity_str)
        if "allApp" in activity_str:
            print("清理应用程序完毕")
            break
        os.popen(f"adb shell am force-stop {activity_str}").read()
    print("清除多余应用进程")
    sleep(10)    

kill_activity()


class GetScreenStatus:
#     def __init__(self):
#         off_ststus = os.popen(screen_status_off).read()
#         print("init 设备初始化处于熄灭状态",off_ststus)
    
    @classmethod
    def get_screen_status(self):
        screen_status = os.popen(screen_status_cmd).read().split("=")[1]
        return screen_status
    
    
    def test_check_normal_screne(self):
        #亮屏---开始计时  5分钟后获取屏幕状态 -----灭屏状态
        try:
            sleep(5)
            on_ststus = os.popen(screen_status_on)
            print("case1 发送亮屏信号")  
            sleep(2)
            on_status = self.get_screen_status()
            sleep(2)
            if "true" in on_status:
                print("case1 设备已经处于亮屏状态")
            print("----开始等待7分钟----")
            sleep(7*60)
            print("----结束7分钟")
            current_status = self.get_screen_status()
            sleep(2)
            if "false" in current_status:
                print("case1 设备已经处于自己息屏状态")
                assert_equal("1", "1", "设备5分钟后自己息屏")
            elif "true" in current_status:
                print("error case1 设备5分钟后处于亮屏状态")
                assert_equal("1", "2", "设备5分钟后依旧没息屏")
                
            sleep(10)
        except Exception as e:
            print("case1 error ",e)
        
        
    def test_douyin_check_screen(self):
        try:
            kill_activity()
            sleep(5)
            on_ststus = os.popen(screen_status_on)
            print("case2 抖音发送亮屏信号")  
            on_status = self.get_screen_status()
            sleep(2)
            if "true" in on_status:
                print("case2 设备已经处于亮屏状态")
            sleep(5)
            start_app("com.ss.android.ugc.aweme")           
            print("case2 启动抖音app")
            sleep(5)
            ST.SAVE_IMAGE = True
            for i in range(2):
                
                if exists(Template(r"tpl1690801118885.png", record_pos=(-0.038, 0.04), resolution=(1920, 1200))):
                    touch(Template(r"tpl1690801118885.png", record_pos=(-0.038, 0.04), resolution=(1920, 1200)))

                sleep(2)
                if exists(Template(r"tpl1689843527245.png", record_pos=(-0.001, 0.06), resolution=(1920, 1200))):
                    touch(Template(r"tpl1689843527245.png", record_pos=(-0.001, 0.06), resolution=(1920, 1200)))
                sleep(1)
                if exists(Template(r"tpl1689843724839.png", record_pos=(-0.039, 0.034), resolution=(1920, 1200))):
                    touch(Template(r"tpl1689843724839.png", record_pos=(-0.039, 0.034), resolution=(1920, 1200)))
                sleep(2)    
                if exists(Template(r"tpl1694421241743.png", record_pos=(-0.002, 0.02), resolution=(1920, 1200))):
                    touch(Template(r"tpl1694421241743.png", record_pos=(-0.002, 0.02), resolution=(1920, 1200)))
                sleep(2)
                if exists(Template(r"tpl1693367979601.png", record_pos=(-0.001, 0.011), resolution=(1920, 1200))):
                    touch(Template(r"tpl1693367979601.png", record_pos=(-0.001, 0.011), resolution=(1920, 1200)))

                sleep(1)
                if exists(Template(r"tpl1689844169306.png", record_pos=(0.257, -0.027), resolution=(1920, 1200))):
                    touch(Template(r"tpl1689844169306.png", record_pos=(0.257, -0.027), resolution=(1920, 1200)))
                sleep(1)
                if exists(Template(r"tpl1693568509778.png", record_pos=(-0.261, -0.198), resolution=(1920, 1200))):
                    touch(Template(r"tpl1693568509778.png", record_pos=(-0.261, -0.198), resolution=(1920, 1200)))

                sleep(1)
                if exists(Template(r"tpl1693225378871.png", record_pos=(0.0, 0.01), resolution=(1920, 1200))):
                    touch(Template(r"tpl1693225378871.png", record_pos=(0.0, 0.01), resolution=(1920, 1200)))

            print("case2 ----开始等待7分钟----")
            sleep(7*60)
            ST.SAVE_IMAGE = True
            print("case2 ----结束7分钟")
            current_status = self.get_screen_status()
            sleep(2)
            if "false" in current_status:
                print("case2 播放抖音6分钟后设备已经处于自己息屏状态")
                assert_equal("1", "2", "case2 抖音播放5分钟后自己灭屏了")
                
            elif "true" in current_status:
                print("case2 播放抖音,设备6分钟后处于亮屏状态")
                assert_equal("1", "1", "case2 抖音播放5分钟后还在亮屏呢")

            sleep(5)
            print("case2 杀掉抖音后等待7分钟")
            kill_activity()
            sleep(5)
            print("case2----开始等待7分钟----")
            sleep(7*60)
            print("case2----结束7分钟")
            current_status = self.get_screen_status()
            sleep(2)
            if "false" in current_status:
                print("case2 杀掉抖音6分钟后设备已经处于自己息屏状态")
                assert_equal(1,1, "case2 关闭应用后自己息屏.")

            elif "true" in current_status:
                print("关闭抖音,设备6分钟后处于亮屏状态")
                assert_equal(1,2, "case2 关闭应用后并没息屏")
            sleep(5)
        except:pass
        finally:
            kill_activity()
        
    def run_report(self):
        a = 1
        while a<500:
            self.test_check_normal_screne()         
            self.test_douyin_check_screen()
            sleep(5)
            from airtest.report.report import simple_report
            simple_report(__file__,logpath=True,output="log"+str(a)+".html")
            a = a+1

        
if __name__=="__main__":
    gs = GetScreenStatus()
    gs.run_report()    
    


















